package server

import (
	"fmt"
	"log"

	"github.com/TarsCloud/TarsGo/tars"
	"github.com/TarsCloud/TarsGo/tars/util/conf"
	hypernexusclient "github.com/wzh200x/hyperNexus/hyperNexusClient"
)

type ReportServer struct {
	NatsService *NatsService // NATS服务
	// 可以在这里添加需要的服务器组件
	DbAgentServerClient *hypernexusclient.DbAgentServerClient
}

func (rs *ReportServer) Init(comm *tars.Communicator, sConf *conf.Conf) error {

	// 初始化DbAgentServerClient
	rs.DbAgentServerClient = hypernexusclient.NewDbAgentServerClient()
	err := rs.DbAgentServerClient.Init(comm)
	if err != nil {
		tars.GetLogger("").Errorf("Failed to initialize DbAgentServerClient: %v", err)
		return err
	}

	// 初始化NATS服务
	rs.NatsService = NewNatsService()
	err = rs.NatsService.Init(comm, sConf)
	if err != nil {
		log.Printf("初始化NATS服务失败: %v\n", err)
		return err
	}

	return nil
}

func (rs *ReportServer) InitFinish() error {
	// 完成NATS服务初始化
	if rs.NatsService != nil {
		err := rs.NatsService.InitFinish()
		if err != nil {
			log.Printf("完成NATS服务初始化失败: %v\n", err)
			return err
		}
		log.Println("NATS服务初始化完成")
	}

	return nil
}

func (rs *ReportServer) Name() string {
	return "reportServer"
}

func (rs *ReportServer) Info() string {
	return fmt.Sprintf(`server :%s version :%s`, "reportServer", "0.0.1")
}

// Stop 停止服务
func (rs *ReportServer) Stop() error {
	// 关闭NATS服务
	if rs.NatsService != nil {
		rs.NatsService.Close()
		log.Println("NATS服务已关闭")
	}
	return nil
}

// Close 关闭服务
func (rs *ReportServer) Close() {
	// 关闭NATS服务
	if rs.NatsService != nil {
		rs.NatsService.Close()
		log.Println("NATS服务已关闭")
	}
}

var ServerApp = &ReportServer{}
